package com.sunda.spmsweb.overseacontroller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsoversea.dto.OverseaDeliveryNoteCompleteDTO;
import com.sunda.spmsoversea.dto.OverseaDeliveryNotePageDTO;
import com.sunda.spmsoversea.service.IDataFromSapService;
import com.sunda.spmsoversea.service.IOverseaDeliveryNoteService;
import com.sunda.spmsuser.service.ISpmsUserService;
import com.sunda.spmsweb.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 海外仓交货单表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-10-22
 */
@Slf4j
@RestController
@RequestMapping("/overseaDeliveryNote")
@Api(tags = "海外仓交货单任务", description = "海外仓交货单任务")
public class OverseaDeliveryNoteController {

    @Autowired
    IOverseaDeliveryNoteService iOverseaDeliveryNoteService;

    @Autowired
    IDataFromSapService iDataFromSapService;

    @Autowired
    ISpmsUserService iSpmsUserService;

    @RequestMapping("/updateDeliveryNoteRest")
    @ApiOperation(value = "从SAP实时更新交货单及详情", notes = "从SAP实时更新交货单及详情\n" +
            "实时更新交货单、装箱单等数据，示例参数：装箱单号 PL00038239；交货日期 2021-11-20；\n", httpMethod = "POST")
    public ResponseResult updateDeliveryNoteRest(String sapPackageList, String deliveryDate){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iDataFromSapService.updateDeliveryNoteRest(sapPackageList, deliveryDate, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOverseaDeliveryNote")
    @ApiOperation(value = "查询交货单表头分页", notes = "查询交货单表头分页\n" +
            "查询交货单表头分页，示例参数：\n" +
            "{\n" +
            "\t\"cabinetNumber\": \"\",\n" +
            "\t\"current\": 1,\n" +
            "\t\"sapDeliveryNote\": \"\",\n" +
            "\t\"sapPackageList\": \"\",\n" +
            "\t\"size\": 20,\n" +
            "\t\"spmsStatus\": \"\",\n" +
            "\t\"werksDelivery\": \"\",\n" +
            "\t\"werksReceive\": \"\"\n" +
            "}", httpMethod = "POST")
    public ResponseResult getOverseaDeliveryNote(@RequestBody OverseaDeliveryNotePageDTO overseaDeliveryNotePageDTO){
        try {
            IPage iPage = iOverseaDeliveryNoteService.getOverseaDeliveryNote(overseaDeliveryNotePageDTO);
            return ResponseResult.success().add("overseaDeliveryNoteList", iPage);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOverseaDeliveryNotePda")
    @ApiOperation(value = "查询交货单表头分页PDA", notes = "查询交货单表头分页\n" +
            "查询交货单表头分页，spmsStatus必输，逗号分割多个值，示例参数：\n" +
            "{\n" +
            "\t\"cabinetNumber\": \"\",\n" +
            "\t\"current\": 1,\n" +
            "\t\"sapDeliveryNote\": \"\",\n" +
            "\t\"sapPackageList\": \"\",\n" +
            "\t\"size\": 20,\n" +
            "\t\"spmsStatus\": \"1,2\",\n" +
            "\t\"werksDelivery\": \"\",\n" +
            "\t\"werksReceive\": \"\"\n" +
            "}", httpMethod = "POST")
    public ResponseResult getOverseaDeliveryNotePda(@RequestBody OverseaDeliveryNotePageDTO overseaDeliveryNotePageDTO){
        try {
            IPage iPage = iOverseaDeliveryNoteService.getOverseaDeliveryNotePda(overseaDeliveryNotePageDTO);
            return ResponseResult.success().add("overseaDeliveryNoteList", iPage);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOverseaDeliveryNoteItem")
    @ApiOperation(value = "查询交货单明细物料列表(含损益 无箱码行)", notes = "查询交货单明细物料列表\n" +
            "查询交货单明细，交货单本身的明细行，示例参数：2000000001", httpMethod = "POST")
    public ResponseResult getOverseaDeliveryNoteItem(@RequestParam String sapDeliveryNote){
        try {
            return ResponseResult.success().add("overseaDeliveryNoteItemList", iOverseaDeliveryNoteService.getOverseaDeliveryNoteItem(sapDeliveryNote));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOverseaDeliveryNoteBox")
    @ApiOperation(value = "查询交货单箱码明细列表(无损益 有箱码行)", notes = "查询交货单明细箱码列表\n" +
            "查询交货单明细，交货单关联出箱码及箱码明细行号关系，示例参数：2000000001", httpMethod = "POST")
    public ResponseResult getOverseaDeliveryNoteBox(@RequestParam String sapDeliveryNote){
        try {
            return ResponseResult.success().add("overseaDeliveryNoteBoxList", iOverseaDeliveryNoteService.getOverseaDeliveryNoteBox(sapDeliveryNote));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }


     @RequestMapping("/getOverseaDnOverOrUnderCharged")
     @ApiOperation(value = "获取交货单实际收货信息(含损益)", notes = "获取交货单实际收货信息(含损益)\n" +
             "sapDeliveryNote = 2000000001, 2000000002", httpMethod = "POST")
     public ResponseResult getOverseaDnOverOrUnderCharged(@RequestParam String sapDeliveryNote){
         try {
            return ResponseResult.success().add("overseaPackageListItem",
                    iOverseaDeliveryNoteService.getOverseaDnOverOrUnderCharged(sapDeliveryNote));
         }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
         }
     }

//    @RequestMapping("/overseaDeliveryNoteToOa")
//    @ApiOperation(value = "交货单提交OA", notes = "交货单提交OA\n" +
//            "sapDeliveryNote = 2000000001, 2000000002", httpMethod = "POST")
//    public ResponseResult overseaDeliveryNoteToOa(@RequestBody OverseaDeliveryNoteOaDTO overseaDeliveryNoteOaDTO){
//        try {
//            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
//            SpmsUser spmsUser = iSpmsUserService.getByUserId(userId);
//            return iOverseaDeliveryNoteService.overseaDeliveryNoteToOa(overseaDeliveryNoteOaDTO, spmsUser);
//        }catch (Exception e){
//            e.printStackTrace();
//            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
//        }
//    }

    @RequestMapping("/overseaWhsInCompleted")
    @ApiOperation(value = "确认入库完成", notes = "确认入库完成\n" +
            "海外收货任务入库完成。状态变化：0-0; 0-2; 1-1; 1-2; " +
            "前提是不存在未完成的入库任务；\n" +
            "如果存在少收，将少收信息拼接成 实收入库(Z_SPMS_MIGO) 提交SAP，SPMS无库存操作，成功则更新实收损益和 入库凭证 等信息 0-2/1-2；失败记日志 0-0/1-1；\n" +
            "", httpMethod = "POST")
    public ResponseResult overseaDeliveryNoteToOa(@RequestBody OverseaDeliveryNoteCompleteDTO overseaDeliveryNoteCompleteDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaDeliveryNoteService.overseaWhsInCompleted(overseaDeliveryNoteCompleteDTO, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

     @RequestMapping("/overseaDeliveryNoteToSap")
     @ApiOperation(value = "交货单提交SAP", notes = "交货单提交SAP(含损益)\n" +
             "修改后SPMS交货单状态设计：\n" +
             "    0待到货/SPMS可从SAP更新单据；√ 状态停留\n" +
             "    1-收货中/SAP凭证撤销；√ 状态停留\n" +
             "    2-入库完成；√ 状态停留\n" +
             "    6-收货多收提交SAP失败；√ 状态停留\n" +
             "    7-收货多收提交SAP成功；—— 中间状态\n" +
             "    8-收货少收提交SAP失败；√ 状态停留\n" +
             "    9-收货少收提交SAP成功；—— 中间状态\n" +
             "    10-提交SAP完成；—— 中间状态\n" +
             "    11-收货完成；√ 状态停留\n" +
             "    12-关闭/废单;√ 状态停留\n" +
             "单据状态变化：\n" +
             "    2-6; 2-7; 2-8; 2-9; 2-10; 2-11;\n" +
             "    6-6; 6-7; 6-8; 6-9; 6-10; 6-11;\n" +
             "    8-8; 8-9; 8-10; 8-11;\n" +
             "盘盈提交SAP 701-盘盈，702-盘亏；\n" +
             "20211122改动：交货单控制不允许多收，6、7 状态设置作废。\n" +
             "20211130改动：交货单提交SAP表头增加成本中心字段，必输\n" +
             "sapDeliveryNote = 2000000001, 2000000002", httpMethod = "POST")
     public ResponseResult overseaDeliveryNoteToSap(@RequestParam String sapDeliveryNote,
                                                    @RequestParam String underPostingDate,
                                                    @RequestParam Integer dataVersion,
                                                    @RequestParam String costCenter){
        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
        return iOverseaDeliveryNoteService.overseaDeliveryNoteToSap(sapDeliveryNote, underPostingDate, dataVersion, costCenter, userId);
     }

     @RequestMapping("/cancelOverseaPackageList")
     @ApiOperation(value = "交货单撤销", notes = "交货单撤销\n" +
             "sapDeliveryNote = 2000000001, cancelPostingDate = 2021-10-18\n" +
             "修改后SPMS交货单状态设计：\n" +
             "    0待到货/SPMS可从SAP更新单据；√ 状态停留\n" +
             "    1-收货中/SAP凭证撤销；√ 状态停留\n" +
             "    2-入库完成；√ 状态停留\n" +
             "    6-收货多收提交SAP失败；√ 状态停留\n" +
             "    7-收货多收提交SAP成功；—— 中间状态\n" +
             "    8-收货少收提交SAP失败；√ 状态停留\n" +
             "    9-收货少收提交SAP成功；—— 中间状态\n" +
             "    10-提交SAP完成；—— 中间状态\n" +
             "    11-收货完成；√ 状态停留\n" +
             "    12-关闭/废单;√ 状态停留\n" +
             "撤销操作单据状态变化：\n" +
             "    1-0; 检查无有效入库任务才能退回到待到货；更新状态、删除实收工厂、实收仓库、实际到货日期、数据状态 等字段；\n" +
             "    2-1; 更新交货单状态；\n" +
             "    8-1; 更新交货单状态；\n" +
             "    11-1; 如果少收凭证字段有值则先撤回少收凭证，有少收凭证时才需要填写撤销过账日期字段，没有的直接更新交货单状态；\n" +
             "20211122改动：交货单控制不允许多收，6、7 状态设置作废。" +
             "\n" +
             "\n" +
             "20211206改动：交货单上如果存在少收，完成入库时则需要将少收按实收提交SAP返回入库凭证；撤回时也需要撤回入库凭证；\n" +
             "撤回操作状态及逻辑更新如下：\n" +
             "    1收货中/SAP凭证撤销\n" +
             "        撤销 —— 前提是无有效(非关闭状态)入库任务 1-0；\n" +
             "\n" +
             "    2入库完成\n" +
             "        不存在少收\n" +
             "            撤销 —— 更新交货单状态 2-1；\n" +
             "        存在少收\n" +
             "            撤销 —— 撤回少收的 入库凭证(Z_SPMS_CANCEL)，成功则记录 入库撤销 凭证等信息 2-1；失败记日志 2-2；\n" +
             "\n" +
             "    8收货少收提交SAP失败\n" +
             "        存在少收\n" +
             "            撤销 —— 撤回少收的 入库凭证(Z_SPMS_CANCEL)，成功则记录 入库撤销 凭证等信息 8-1；失败记日志 8-8；\n" +
             "\n" +
             "    11收货完成\n" +
             "        不存在少收\n" +
             "            撤销 —— 11-1；\n" +
             "        存在少收\n" +
             "            撤销 —— 撤回少收的 少收凭证(Z_SPMS_CANCEL)，成功则记录 少收撤销 凭证等信息 11-2；失败记日志 11-11；\n",
     httpMethod = "POST")
     public ResponseResult cancelOverseaPackageList(@RequestParam String sapDeliveryNote,
                                                    String cancelPostingDate,
                                                    String cancelUnderPostingDate,
                                                    @RequestParam Integer dataVersion){
         try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaDeliveryNoteService.cancelOverseaDeliveryNote(sapDeliveryNote, cancelPostingDate, cancelUnderPostingDate, dataVersion, userId);
         }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
         }
     }

    @RequestMapping("/checkOverseaDeliveryNote")
    @ApiOperation(value = "检查交货单是否为可用数据", notes = "检查交货单是否为可用数据\n" +
            "sapDeliveryNote = 2000000001, 0700442729\n" +
            "检查海外收货交货单关联信息是否能被拆分入库任务\n" +
            "1.交货单存在必须有明细行；\n" +
            "2.交货单所属装箱单信息存在；\n" +
            "3.交货单关联装箱单明细必须存在；\n" +
            "4.交货单所有明细行必须能对上装箱单明细行数据；通过 公司间采购订单号 和 公司间采购订单行号；\n" +
            "5.交货单关联出的装箱单明细行，都必须能对上箱码明细行数据；通过 装箱单号 和 装箱单行号；\n" +
            "以上五点都满足则认定为可用数据。",
            httpMethod = "POST")
    public ResponseResult checkOverseaDeliveryNote(@RequestParam String sapDeliveryNote){
        try {
            return iOverseaDeliveryNoteService.checkOverseaDeliveryNote(sapDeliveryNote);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/closeOverseaDeliveryNote")
    @ApiOperation(value = "交货单关闭/删除", notes = "交货单关闭/删除\n" +
            "sapDeliveryNote = 2000000001，交货单状态变化：0-12，其余状态暂不考虑关闭功能",
            httpMethod = "POST")
    public ResponseResult closeOverseaDeliveryNote(@RequestParam String sapDeliveryNote,
                                                   @RequestParam Integer dataVersion){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaDeliveryNoteService.closeOverseaDeliveryNote(sapDeliveryNote, dataVersion, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/overseaDeliveryNoteDesign")
    @ApiOperation(value = "交货单各状态及功能设计", notes = "交货单各状态及功能设计\n" +
            "SPMS收货单据状态设计(\n" +
            "    默认0待到货; —— 停留状态\n" +
            "    1收货中/SAP凭证撤销; —— 停留状态\n" +
            "    2入库完成/提交OA成功;  —— 停留状态\n" +
            "    3提交OA失败; —— 废弃\n" +
            "    4OA审批不通过; —— 废弃\n" +
            "    5OA审批通过/提交SAP失败; —— 废弃\n" +
            "    6收货多收提交SAP失败; —— 废弃\n" +
            "    7收货多收提交SAP成功; —— 废弃\n" +
            "    8收货少收提交SAP失败; —— 停留状态\n" +
            "    9收货少收提交SAP成功; —— 过程状态\n" +
            "    10提交SAP完成; —— 过程状态\n" +
            "    11收货完成; —— 停留状态\n" +
            "    12关闭/废单;) —— 停留状态\n" +
            "\n" +
            "一共有六个按钮，用户能看到和有操作的状态为：0，1，2，8，11(完结状态)；\n" +
            "\n" +
            "各状态操作按钮处理及状态变化：\n" +
            "    0待到货;\n" +
            "        更新 —— 从SAP刷新交货单数据 0-0；\n" +
            "        关闭 —— 废弃 0-12；\n" +
            "        生成入库任务 —— 生成入库任务 0-1；\n" +
            "        i.不存在少收\n" +
            "            无\n" +
            "        ii.存在少收\n" +
            "            入库完成 —— 前提是不存在未完成的入库任务，将少收信息拼接成 实收入库(Z_SPMS_MIGO) 提交SAP，成功则更新实收损益和 入库凭证 等信息 0-2；失败记日志 0-0；\n" +
            "\n" +
            "    1收货中/SAP凭证撤销;\n" +
            "        生成入库任务 —— 生成分批入库任务 1-1；\n" +
            "        撤销 —— 前提是无有效入库任务 1-0；\n" +
            "        i.不存在少收\n" +
            "            入库完成 —— 前提是不存在未完成的入库任务，更新实收及损益信息 1-2；\n" +
            "        ii.存在少收\n" +
            "            入库完成 —— 前提是不存在未完成的入库任务，将少收信息拼接成 实收入库(Z_SPMS_MIGO) 提交SAP，成功则更新实收损益和 入库凭证 等信息 1-2；失败记日志 1-1；\n" +
            "      \n" +
            "    2入库完成; \n" +
            "        i.不存在少收\n" +
            "            提交SAP —— 直接收货完成 2-11；\n" +
            "            撤销 —— 更新交货单状态 2-1；\n" +
            "        ii.存在少收\n" +
            "            提交SAP —— 将少收信息拼接成 少收(Z_SPMS_ANOMALY_POST) 提交SAP，成功则记录 少收凭证 2-11；失败记日志 2-8；\n" +
            "            撤销 —— 撤回少收的 入库凭证(Z_SPMS_CANCEL)，成功则记录 入库撤销 凭证等信息 2-1；失败记日志 2-2；\n" +
            "\n" +
            "    8收货少收提交SAP失败; \n" +
            "        i.不存在少收\n" +
            "            无\n" +
            "        ii.存在少收\n" +
            "            提交SAP —— 将少收信息拼接成 少收(Z_SPMS_ANOMALY_POST) 提交SAP，成功则记录少收凭证 8-11；失败记日志 8-8；\n" +
            "            撤销 —— 撤回少收的 入库凭证(Z_SPMS_CANCEL)，成功则记录 入库撤销 凭证等信息 8-1；失败记日志 8-8；\n" +
            "\n" +
            "    11收货完成; \n" +
            "        i.不存在少收\n" +
            "            撤销 —— 11-1；\n" +
            "        ii.存在少收\n" +
            "            撤销 —— 撤回少收的 少收凭证(Z_SPMS_CANCEL)，成功则记录 少收撤销 凭证等信息 11-2；失败记日志 11-11；\n" +
            "\n" +
            "    12-关闭/废单;\n" +
            "        无",
            httpMethod = "POST")
    public ResponseResult overseaDeliveryNoteDesign(){
        try {
            return ResponseResult.success("文档设计接口");
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }


    @RequestMapping("/getOverdueDeliveryNote")
    @ApiOperation(value = "获取到货超14天未完结单据", notes = "获取到货超14天未完结单据\n" +
            "werks = TF02，可选输入；",
            httpMethod = "POST")
    public ResponseResult getOverdueDeliveryNote(String werks){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return ResponseResult.success().add("overdueDeliveryNoteList", iOverseaDeliveryNoteService.getOverdueDeliveryNote(werks));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
}
